﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE.Mods
{
    partial class FrmMConfigNew : Form
    {
        public FrmMConfigNew()
        {
            InitializeComponent();
            m_lstConfig = new List<ClsDataConfig>();
            m_cfgBuilder = new ClsDataConfigBuilder(GDefine.DBNameMOD);
            this.checkEffitive.Checked = true;
            isNew = true;
        }

        public FrmMConfigNew(ClsDataConfigBuilder builder, List<ClsDataConfig> lstConfig)
        {
            InitializeComponent();
            m_lstConfig = lstConfig;
            m_cfgBuilder = builder;

            this.textProcode.Text = m_cfgBuilder.sProcode;
            this.textProcode.Enabled = false;
            this.nudConfigVer.Value = Convert.ToDecimal(m_cfgBuilder.sVerConfig);
            this.textSpecVer.Text = m_cfgBuilder.sVerSpec;
            isNew = false;
        }

        ClsDataConfigBuilder m_cfgBuilder;
        List<ClsDataConfig> m_lstConfig;
        ClsWebRecord m_resultObj;
        DataTable m_dtConfig;
        bool isNew;

        private void FrmMConfigNew_Load(object sender, EventArgs e)
        {
            nudConfigVer.Enabled = false;
            m_resultObj = new ClsWebRecord(null);
            ctl_config_dgvData.RowHeadersVisible = false;
            ctl_config_dgvData.AllowUserToAddRows = false;
            ctl_config_dgvData.ReadOnly = true;
            ctl_config_dgvData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            for (int i = 0; i < this.ctl_config_dgvData.Columns.Count; i++)
            {
                this.ctl_config_dgvData.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            m_dtConfig = new DataTable();
            var list = ClsDataConfigBuilder.getConfigColNames();
            for (int i = 2; i < list.Count; i++)
            {//构建表
                m_dtConfig.Columns.Add(list[i]);
            }

            ctl_config_dgvData.DataSource = m_dtConfig;

            for (int i = 0; i < m_lstConfig.Count; i++)
            {//赋值
                DataRow row = m_dtConfig.NewRow();
                m_lstConfig[i].setRow(row);
                m_dtConfig.Rows.Add(row);
            }

            List<string> cols = ClsDataConfigBuilder.getConfigColNames();
            ctl_config_dgvData.Columns[cols[28]].Visible = false;
            ctl_config_dgvData.Columns[cols[29]].Visible = false;
            ctl_config_dgvData.Columns[cols[30]].Visible = false;
            ctl_config_dgvData.Columns[cols[31]].Visible = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (nudConfigVer.Value == 0 || textSpecVer.Text == "" || textProcode.Text == "")
            {
                MessageBox.Show("代码为空 或 版本为0，请重新输入！");
                return;
            }

            if (!m_resultObj.IsValidProductTypeID(textProcode.Text))
            {
                MessageBox.Show("当前产品代码无效，请重新输入！");
                return;
            }

            if (m_lstConfig == null || m_lstConfig.Count == 0)
            {
                MessageBox.Show("没有配置！");
                return;
            }

            m_cfgBuilder.constructByProcode(textProcode.Text);
            if (m_cfgBuilder.construct(textProcode.Text, nudConfigVer.Value.ToString("0.0")))
            {
                MessageBox.Show("当期代码和配置版本已存在！");
                return;
            }

            string error = "";
            if (isNew)
            {
                DateTime efftime = DateTime.Now;
                ProcodeINFO procodeObj = new ProcodeINFO();
                procodeObj.sSpecVer = textSpecVer.Text;
                procodeObj.sConfigVer = nudConfigVer.Value.ToString("0.0");
                procodeObj.sProCode = textProcode.Text;
                procodeObj.sEffTime = efftime.ToString();
                procodeObj.bEffective = checkEffitive.Checked;
                procodeObj.sRemark = "";

                List<string> cols = ClsDataConfigBuilder.getConfigColNames();
                for (int i = 0; i < m_dtConfig.Rows.Count; i++)
                {
                    m_dtConfig.Rows[i][cols[28]] = procodeObj.sConfigVer;
                    m_dtConfig.Rows[i][cols[29]] = procodeObj.sSpecVer;
                    m_dtConfig.Rows[i][cols[30]] = procodeObj.sEffTime;
                    m_dtConfig.Rows[i][cols[31]] = procodeObj.bEffective;
                    m_lstConfig[i].ConfigVer = procodeObj.sConfigVer;
                    m_lstConfig[i].SpecVer = procodeObj.sSpecVer;
                    m_lstConfig[i].EffTime = efftime;
                    m_lstConfig[i].Effective = procodeObj.bEffective;
                }

                if (m_cfgBuilder.upload(procodeObj, m_lstConfig, ref error))
                {
                    MessageBox.Show("保存成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            else
            {
                if (m_cfgBuilder.update(checkEffitive.Checked, m_lstConfig))
                {
                    MessageBox.Show("更新成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("更新失败");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void ctl_config_btnCopy_Click(object sender, EventArgs e)
        {
            if (ctl_config_dgvData.CurrentRow == null)
            {
                toolStatus.Text = "提示: 请选择行!";
                return;
            }
            int n = ctl_config_dgvData.CurrentRow.Index;

            DataRow row = m_dtConfig.NewRow();
            for (int i = 0; i < m_dtConfig.Columns.Count; i++)
                row[i] = m_dtConfig.Rows[n][i];

            FrmConfig frmCfg = new FrmConfig(row);
            if (frmCfg.ShowDialog() == DialogResult.OK)
            {
                m_dtConfig.Rows.Add(row);
                var config = new ClsDataConfig();
                config.setValues(0, textProcode.Text, row);
                m_lstConfig.Add(config);
                toolStatus.Text = "提示: 复制并新增成功!";
            }
            else
            {
                row.Delete();
                toolStatus.Text = "提示: 复制并新增已取消!";
            }
        }

        private void ctl_config_btnAdd_Click(object sender, EventArgs e)
        {
            DataRow row = m_dtConfig.NewRow();
            row[20] = "dBm";
            row[26] = textSpecVer.Text;
            row[27] = nudConfigVer.Value.ToString("0.0");
            row[28] = DateTime.Now.ToString();
            row[29] = checkEffitive.Checked;

            FrmConfig frmCfg = new FrmConfig(row);
            if (frmCfg.ShowDialog() == DialogResult.OK)
            {
                m_dtConfig.Rows.Add(row);
                var config = new ClsDataConfig();
                config.setValues(0, textProcode.Text, row);
                m_lstConfig.Add(config);
                toolStatus.Text = "提示: 新增成功!";
            }
            else
            {
                row.Delete();
                toolStatus.Text = "提示: 新增已取消!";
            }
        }

        private void ctl_config_btnModify_Click(object sender, EventArgs e)
        {
            if (ctl_config_dgvData.CurrentRow == null)
            {
                toolStatus.Text = "提示: 请选择行!";
                return;
            }
            int n = ctl_config_dgvData.CurrentRow.Index;

            FrmConfig frmCfg = new FrmConfig(m_dtConfig.Rows[n]);
            if (frmCfg.ShowDialog() == DialogResult.OK)
            {
                var config = new ClsDataConfig();
                config.setValues(0, textProcode.Text, m_dtConfig.Rows[n]);
                m_lstConfig[n] = config;
                toolStatus.Text = "提示: 修改成功!";
            }
            else
            {
                toolStatus.Text = "提示: 修改已取消!";
            }
        }

        private void ctl_config_btnDelete_Click(object sender, EventArgs e)
        {
            if (ctl_config_dgvData.CurrentRow == null)
            {
                toolStatus.Text = "提示: 请选择行!";
                return;
            }
            if (MessageBox.Show("确认删除？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int n = ctl_config_dgvData.CurrentRow.Index;
                m_dtConfig.Rows.RemoveAt(n);
                m_lstConfig.RemoveAt(n);
                toolStatus.Text = "提示: 删除成功!";
            }
            else
            {
                toolStatus.Text = "提示: 删除已取消!";
            }
        }

        private void btnCheckCode_Click(object sender, EventArgs e)
        {
            if (!m_resultObj.IsValidProductTypeID(textProcode.Text))
            {
                MessageBox.Show("当前产品代码无效，请重新输入！");
            }
            else
            {
                MessageBox.Show("当前产品代码可以使用！");
            }

            //获取最大版本号
            var listVer = m_cfgBuilder.constructByProcode(textProcode.Text);
            float fMaxVer = float.MinValue;
            for(int i=0;i < listVer.Length; i++)
            {
                float fNowVer = Convert.ToSingle(listVer[i]);
                if (fNowVer > fMaxVer)
                    fMaxVer = fNowVer;
            }
            if (fMaxVer == float.MinValue)
            {
                MessageBox.Show("这是此产品代码的第一个配置版本!");
                fMaxVer = 1.0f;
            }
            else
            {
                fMaxVer += 0.1f;
            }
            nudConfigVer.Value = Convert.ToDecimal(fMaxVer);

        }

    }
}
